﻿using System;
using System.Collections.Generic;
using System.Linq;

using Csla;
using Csla.Core;

using Haushaltsbuch.DataAcces;
using Haushaltsbuch.DataAcces.Entities;

namespace Haushaltsbuch.Domain.Users
{
    [Serializable]
    public class RoleEditList : BusinessListBase<RoleEditList, RoleEdit>
    {
        public static RoleEditList Get()
        {
            return DataPortal.FetchChild<RoleEditList>();
        }

        internal static RoleEditList Get(Role[] entity)
        {
            return DataPortal.FetchChild<RoleEditList>(entity);
        }

        private void Child_Fetch(Role[] entity)
        {
            this.RaiseListChangedEvents = false;

            this.AddRange(entity.Select(RoleEdit.Get).ToArray());

            this.RaiseListChangedEvents = true;
        }

        private void Child_Fetch()
        {
            using (var ctx = new HaushaltsbuchContext())
            {
                var roles = ctx.Roles;

                this.RaiseListChangedEvents = false;

                this.AddRange(roles.Select(RoleEdit.Get).ToArray());

                this.RaiseListChangedEvents = true;

            }
        }
    }
}